package com.activiti.dao.hibernate;

import com.activiti.dao.ActivitiDao;
import com.activiti.dao.model.*;
import com.erp.hr.dao.model.HrDepartment;
import com.erp.hr.dao.model.HrDepartmentCO;
import com.erp.hr.dao.model.HrStaff;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ActivitiDaoImpl implements ActivitiDao {

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(Activiti_TaskInfo obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(Activiti_TaskInfo obj) {
        this.daoSupport.updateDataTransaction(obj);
    }

    @Override
    public void insertOrUpdateDataObject(Activiti_TaskInfo obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(Activiti_TaskInfo obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<Activiti_TaskInfo> getDataObjects() {
        return this.daoSupport.getDataAllObject(Activiti_TaskInfo.class);
    }
    //根据任务ID查询流程信息
    @Override
    public Activiti_TaskInfo getDataObject(int id) {
        return (Activiti_TaskInfo)this.daoSupport.getDataObject(Activiti_TaskInfo.class, id);
    }
    //根据任务ID查询流程信息
    @Override
    public Activiti_TaskInfo getDataObject(String id) {
        String sql = "select d.* from activiti_taskinfo d where d.id = :id";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", id);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("d", HrDepartment.class);
        
        List<Activiti_TaskInfo> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return list.get(0);
        }
        
        return null;
    }

    @Override
    public List<Activiti_TaskInfo> getDataObjects(Activiti_TaskInfo activiti_taskInfo) {
       return this.daoSupport.getDataAllObject(Activiti_TaskInfo.class);
    }

    @Override
    public List<Activiti_TaskInfo> getDataObjects(Pages pages) {
        return null;
    }

    @Override
    public List<Activiti_TaskInfo> getDataObjects(Pages pages, Activiti_TaskInfo activiti_taskInfo) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, Activiti_TaskInfo activiti_taskInfo) {
        return null;
    }


    
    @Override
    @Permissions(PermissionType.OWN)
    public List<Activiti_TaskInfo> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, Activiti_TaskInfo paramObj) {
        return null;
    }


    @Override
    public List getHrTaskInfoAndSunTaskdList(String id) {
        String sql = "SELECT a.*,b.* from activiti_sub_taskinfo a " +
                "left join activiti_taskinfo b on a.id=b.id " +
                "where a.id= :id order by a.proc_time desc,a.proc_order desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("id", id);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("a", Activiti_TaskInfoRO.class);
        entity.put("b", Activiti_SubTaskInfoRO.class);
        List list = this.daoSupport.selectDataSql(sql, entity, args);

        return list;
    }
    public List<Activiti_TaskInfoRO> getDataObjects(List<TaskVo> procInstIdList){
        String parmList="";
        Map<String, Object> args = new HashMap<String, Object>();
        for(int i=0;i<procInstIdList.size();i++){
            TaskVo taskVoTmp=procInstIdList.get(i);
            parmList=parmList+":instId"+i+",";
            args.put("instId"+i, procInstIdList.get(i).getProcessInstanceId());
        }
        parmList=parmList.substring(0,parmList.length()-1);
        String sql = "select a.id,a.task_id,a.proc_inst_id,a.proc_name,a.staff_code,a.company_code,a.department_code,a.create_time,a.status from activiti_taskinfo a where a.proc_inst_id in (" +parmList+ ") order by a.create_time desc";
        //List<String> str = Arrays.asList(procInstIdList.split(","));
        //args.put("proc_inst_id", str);
        List list = this.daoSupport.selectDataSql(sql, Activiti_TaskInfoRO.class, args);
        return list;
    }
    @Override
    public void setProceStatus(String id) {

    }

    public Activiti_TaskCount queryTaskCount(String staffCode) {
        String sql = "SELECT " +
                "count(case when a.status=1  then 1 end) waitCount," +
                "count(case when a.status=2  then 2 end) workIngCount," +
                "count(case when a.status=3  then 3 end) backCount," +
                "count(case when a.status=4  then 4 end) finshedCount from activiti_taskinfo a where a.staff_code=:staffCode";

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("staffCode", staffCode);
        List<Activiti_TaskCount> list = this.daoSupport.selectDataSql(sql, Activiti_TaskCount.class, args);
        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public Activiti_TaskInfo queryActivitiTaskInfoByPid(String proc_inst_id){
        String sql = "select p.* from activiti_taskinfo p where p.proc_inst_id = :proc_inst_id";

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("proc_inst_id", proc_inst_id);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", Activiti_TaskInfo.class);

        List<Activiti_TaskInfo> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return list.get(0);
        }

        return null;
    }
}
